import 'package:flutter/material.dart';
import 'package:flutter_app/components/search_input.dart';
import 'package:flutter_app/views/login_page/login_page.dart';
import 'package:flutter_app/views/user_page/user_page.dart';

class HomePage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  int _currentIndex = 0;
  String appBarTitle;

  List<Widget> _list = [];
  List tabData = [
    {'text': '业界动态', 'icon': Icons.language},
    {'text': 'WIDGET', 'icon': Icons.extension},
    {'text': '关于手册', 'icon': Icons.import_contacts},
    {'text': '个人中心', 'icon': Icons.account_circle}
  ];

  List<BottomNavigationBarItem> _myTabs = [];

  @override
  void initState() {
    super.initState();
    print('widget.userinfo');
    for (int i = 0; i < tabData.length; i++) {
      _myTabs.add(BottomNavigationBarItem(
        icon: Icon(
          tabData[i]['icon'],
        ),
        label: tabData[i]['text'],
      ));
    }

    appBarTitle = tabData[0]['text'];

    _list..add(LoginPage())..add(UserPage());
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: renderAppBar(context, widget),
      body: IndexedStack(
        index: _currentIndex,
        children: _list,
      ),
      bottomNavigationBar: BottomNavigationBar(
        items: _myTabs,
        currentIndex: _currentIndex,
        onTap: _itemTapped,
        type: BottomNavigationBarType.fixed,
        fixedColor: Theme.of(context).primaryColor,
      ),
    );
  }

  Widget renderAppBar(BuildContext context, HomePage widget) {
    return AppBar(
      title: buildSearchInput(context),
    );
  }

  buildSearchInput(BuildContext context) {
    return SearchInput();
  }

  void _itemTapped(int index) {
    setState(() {
      _currentIndex = index;
      appBarTitle = tabData[index]['text'];
    });
  }

}
